# -*- utf-8 -*-
"""
echarts工具类文件包含了大部分常用的echarts配置，配合ajson解析作为更便于阅读的一种图形方法
"""
from ajson import aj

from utils import others


class Config:
    @staticmethod
    def value(**kwargs):
        return kwargs


class Color:
    @staticmethod
    def rgb(r=0, g=0, b=0):
        return f'rgb({r},{g},{b})'

    @staticmethod
    def rgba(r=0, g=0, b=0, a=1):
        return f'rgba({r},{g},{b},{a})'

    @staticmethod
    def hex(hex_color):
        if hex_color[0] == '#':
            return hex_color
        return f'#{hex_color}'

    @staticmethod
    def white():
        return "#FFFFFF"

    @staticmethod
    def red():
        return "#F56C6C"

    @staticmethod
    def green():
        return "#67C23A"

    @staticmethod
    def blue():
        return "#409EFF"

    @staticmethod
    def black():
        return "#303133"

    @staticmethod
    def gray():
        return '#344b58'


class Number:
    def __init__(self, num):
        self.num = num

    def __repr__(self):
        return str(self.num)

    @property
    def value(self):
        return self.num

    def __str__(self):
        return str(self.num)


class Title:
    def __init__(self, text=None, x=20, top=20, textStyle=None, subTextStyle=None, **kwargs):
        if subTextStyle is None:
            subTextStyle = Config.value(color=Color.white(), fontSize=Number(16).value)
        if textStyle is None:
            textStyle = Config.value(color=Color.white(), fontSize=Number(16).value)
        self.text = text
        self.x = x
        self.top = top
        self.textStyle = textStyle
        self.subtextStyle = subTextStyle
        others.set_kwargs(self, **kwargs)


class Tooltip:
    def __init__(self, trigger='axis', axisPointer=None, **kwargs):
        if axisPointer is None:
            axisPointer = {
                'textStyle': {
                    'color': '#fff'
                }
            }
        self.trigger = trigger
        self.axisPointer = axisPointer
        others.set_kwargs(self, **kwargs)


class Grid:
    def __init__(self,
                 left='5%', right='5%', borderWidth=0, top=150, bottom=95, textStyle=None, **kwargs):
        if textStyle is None:
            textStyle = Config.value(color='#FFFFFF')
        self.left = left
        self.right = right
        self.borderWidth = borderWidth
        self.top = top
        self.bottom = bottom
        self.textStyle = textStyle
        others.set_kwargs(self, **kwargs)


class Legend:
    def __init__(self, x='5%', top='10%', textStyle=None, data=None, **kwargs):
        if data is None:
            data = ['female', 'male', 'average']
        if textStyle is None:
            textStyle = Config.value(color='#90979c')
        self.x = x
        self.top = top
        self.textStyle = textStyle
        self.data = data
        others.set_kwargs(self, **kwargs)


class XAxisItem:
    def __init__(self,
                 type_='category', axisLine=None, splitLine=False, axisTick=False, splitArea=False, data=None,
                 interval=0, **kwargs):
        self.type = type_
        self.axisLine = axisLine or Config.value(lineStyle=Config.value(color='#90979c'))
        self.splitLine = Config.value(show=splitLine)
        self.axisTick = Config.value(show=axisTick)
        self.splitArea = Config.value(show=splitArea)
        self.axisLabel = Config.value(interval=interval)
        self.data = data or []
        others.set_kwargs(self, **kwargs)


class YAxisItem:
    """y轴样式"""

    def __init__(self, type_='value', axisLine=None, splitLine=False, axisTick=False, splitArea=False, **kwargs):
        self.type = type_
        self.axisLine = axisLine or Config.value(lineStyle=Config.value(color='#90979c'))
        self.splitLine = Config.value(show=splitLine)
        self.axisTick = Config.value(show=axisTick)
        self.splitArea = Config.value(show=splitArea)
        self.axisLabel = Config.value(interval=0)
        others.set_kwargs(self, **kwargs)


class DataZoom(list):
    """当数据过长时，显示下方的滚动条"""

    def __init__(self, **kwargs):
        list.__init__([])
        self.append({
            'type': 'inside',
            'show': True,
            'height': 15,
            'start': 1,
            'end': 35
        })
        self.append(
            {
                'show': True,
                'height': 30,
                'xAxisIndex': [
                    0
                ],
                'bottom': 30,
                'start': 10,
                'end': 80,
                'handleSize': '110%',
                'handleStyle': {
                    'color': '#d3dee5'
                },
                'textStyle': {
                    'color': '#fff'
                },
                'borderColor': '#90979c'
            }
        )
        others.set_kwargs(self, **kwargs)


class normal:
    def __init__(self):
        pass


class Normal:
    def __init__(self, color=None,
                 label=None, **kwargs):
        if color is None:
            color = Color.rgba(255, 144, 128, 1)
        if label is None:
            label = Config.value(show=True, textStyle=Color.white(), position='insideTop')
        self.color = color
        self.label = label
        others.set_kwargs(self, **kwargs)


class SeriesItem:
    def __init__(self, name, type_='bar', stack='total', barMaxWidth=35, barGap='10%', itemStyle=None):
        if itemStyle is None:
            itemStyle = Config.value(normal=Normal())
        self.name = name
        self.type = type_
        self.stack = stack
        self.barMaxWidth = barMaxWidth
        self.barGap = barGap
        self.itemStyle = itemStyle


class BaseEcharts:
    def __init__(self, bg_color: str = Color.gray(),
                 title: object = None,
                 tooltip: Tooltip = None,
                 grid: Grid = None,
                 legend: Legend = None,
                 calculable: bool = True,
                 xAxis: list = None,
                 yAxis: list = None,
                 dataZoom: DataZoom = None,
                 series: list = None, **kwargs):
        self.backgroundColor = bg_color
        self.title = title or Title(text='undefined')
        self.tooltip = tooltip
        self.grid = grid
        self.legend = legend
        self.calculable = calculable
        self.xAxis = xAxis
        self.yAxis = yAxis
        self.dataZoom = dataZoom
        self.series = series
        others.set_kwargs(self, **kwargs)


if __name__ == '__main__':
    print(aj.parse(BaseEcharts()))
